<template>
  <div class="moreFunctions">
    <section class="header">
      <span>五险一金计算器</span>
    </section>
    <div class="main-content clearfix">
      <div class="input-box">
        <section>
          <span class="title">
            缴纳城市
            <i>*</i>
          </span>
          <span class="value">
            <el-select v-model="payTheCity" clearable placeholder="请选择">
              <el-option
                v-for="item in addressOptions"
                :key="item.areaCode"
                :label="item.areaName"
                :value="item.areaCode"
              >
              </el-option>
            </el-select>
          </span>
        </section>
      </div>
      <div class="input-box">
        <section>
          <span class="title">
            社保基数
            <i>*</i>
          </span>
          <span class="value">
            <el-input
              placeholder="在这里输入社保基数"
              v-model="sbBaseValue"
              clearable
            ></el-input>
          </span>
          <p class="tip" v-show="socialHint">
            社保基数应在{{ sbBaseStart | baseFilter }}~{{
              sbBaseEnd | baseFilter
            }}之间
          </p>
        </section>
      </div>
      <div class="input-box">
        <section>
          <span class="title">
            公积金基数
            <i>*</i>
          </span>
          <span class="value">
            <el-input
              placeholder="在这里输入公积金基数"
              v-model="gjjBaseValue"
              clearable
            ></el-input>
          </span>
          <p class="tip" v-show="accumulationHint">
            公积金基数应在{{ gjjBaseStart | baseFilter }}~{{
              gjjBaseEnd | baseFilter
            }}之间
          </p>
        </section>
      </div>
    </div>
    <div class="button-box">
      <el-button type="primary" :loading="loading" @click="sum()"
        >计算</el-button
      >
    </div>
    <div class="hint">
      根据国家和地方的社会保险法津法规，城镇职工类社会保险的缴费基数以职工本人上一年月平均工资做为缴纳社会保险费的参考依据，但社会保险费的缴费基数不得低于社保局每年公布的最低值，不得高于最高值。
    </div>
    <section class="header">
      <span>计算结果</span>
    </section>
    <div class="sm-f clearfix">
      <ul>
        <li class="value">{{ sumSocialSecurity | baseFilter }}</li>
        <li>社保合计/元</li>
      </ul>
      <ul>
        <li class="value">+</li>
        <li class="no">站位</li>
      </ul>
      <ul>
        <li class="value">{{ sumPublicAccumulationFund | baseFilter }}</li>
        <li>公积金合计/元</li>
      </ul>
      <ul>
        <li class="value">=</li>
        <li class="no">站位</li>
      </ul>
      <ul>
        <li class="value">{{ sumTotal | baseFilter }}</li>
        <li>五险一金合计/元</li>
      </ul>
    </div>
    <div class="table">
      <el-table
        :data="tableData"
        v-loading="loading"
        ref="table"
        border
        tooltip-effect="dark"
        style="width: 100%"
      >
        <el-table-column prop="paymentName" label="缴纳项目"></el-table-column>
        <el-table-column prop="personScale" label="个人比例"></el-table-column>
        <el-table-column
          prop="personVal"
          label="个人缴纳金额"
          :formatter="moneyFilters"
        ></el-table-column>
        <el-table-column prop="companyScale" label="公司比例"></el-table-column>
        <el-table-column
          prop="companyVal"
          label="公司缴纳金额"
          :formatter="moneyFilters"
        ></el-table-column>
      </el-table>
    </div>
    <!-- <section class="footer">
      <span>个人缴纳合计 {{personalTotal}}</span>
      <span>公司缴纳合计 {{companyTotal}}</span>
    </section> -->
  </div>
</template>

<script>
import base from "@/assets/js/base";
import { requestApiPost } from "../api/api";
import { reqRep } from "../report/reqReport";
import { resRep } from "../report/resReport";
export default {
  name: "moreFunctions",
  data() {
    return {
      loading: false,
      payTheCity: "",
      socialHint: false,
      accumulationHint: false,
      socialSecurity: true,
      accumulationFund: true,
      sbBaseStart: "",
      sbBaseEnd: "",
      gjjBaseStart: "",
      gjjBaseEnd: "",
      sumSocialSecurity: 0,
      sumPublicAccumulationFund: 0,
      sumTotal: 0,
      personalTotal: 0,
      companyTotal: 0,
      sbBaseValue: "",
      gjjBaseValue: "",
      addressOptions: [],
      tableData: [],
    };
  },
  methods: {
    //获取缴纳城市
    getAddress() {
      var reqData = {
        grade: 1,
        isStart: 0,
        current: 1,
        pageSize: 999,
      };
      let reqReport = reqRep({ traCode: "_LIST_AREA", data: reqData });
      requestApiPost(reqReport).then((data) => {
        let resReport = resRep(data);
        if (resReport.getReCode() != "0000") {
          this.$message({
            message: resReport.getReMsg(),
            type: "error",
          });
        } else {
          this.addressOptions = resReport.data.records;
        }
      });
    },
    isClick(num) {
      this.isNumber = num;
    },
    switchFun(i) {
      this.switchIndex = i;
    },
    sum() {
      if (!this.payTheCity) {
        this.$message({
          message: "请选择缴纳城市",
          type: "warning",
          duration: 1500,
        });
        return;
      }
      if (!this.sbBaseValue) {
        this.$message({
          message: "请填写社保基数",
          type: "warning",
          duration: 1500,
        });
        return;
      }
      if (!this.gjjBaseValue) {
        this.$message({
          message: "请填写公积金基数",
          type: "warning",
          duration: 1500,
        });
        return;
      }
      this.loading = true;

      var reqData = {
        areaCode: this.payTheCity,
        socialSecurityBase: this.sbBaseValue,
        publicAccumulationFundBase: this.gjjBaseValue,
      };
      let reqReport = reqRep({
        traCode: "_SOCIAL_SECURITY_CALCULATION",
        data: reqData,
      });
      requestApiPost(reqReport).then((data) => {
        let resReport = resRep(data);
        this.loading = false;
        if (resReport.getReCode() != "0000") {
          this.$message({
            message: resReport.getReMsg(),
            type: "error",
          });
        } else {
          this.sumPublicAccumulationFund =
            resReport.data.sumPublicAccumulationFund;
          this.sumSocialSecurity = resReport.data.sumSocialSecurity;
          this.sumTotal = resReport.data.sumTotal;
          this.tableData = resReport.data.rows;
        }
      });
    },
    moneyFilters(a, b, c) {
      if (c > 0) {
        return c;
      } else if (c < 0) {
        return (
          "-" +
          parseFloat(c.toString().split("-")[1])
        );
      } else {
        return "-";
      }
    },
  },
  filters: {
    baseFilter(c) {
      if (c > 0) {
        return c;
      } else if (c < 0) {
        return (
          "-" +
          parseFloat(c.toString().split("-")[1])
        );
      } else {
        return 0;
      }
    },

    perCentFilter(e) {
      if (e > 0) {
        return e + "%";
      } else {
        return "-";
      }
    },
  },
  created() {
    this.getAddress();
  },
};
</script>
<style scoped>
.header {
  padding: 15px;
  height: 50px;
  box-sizing: border-box;
  font-size: 14px;
  border-bottom: 1px solid #eee;
}
.header i {
  margin-right: 5px;
}
.header i:hover {
  color: #13cb6e;
}
.header span {
  cursor: pointer;
}
.main-content {
  padding: 30px 30px 10px;
}
h2 {
  display: block;
  color: #333;
  font-size: 16px;
  margin: 30px 0;
  clear: both;
}
.input-box {
  float: left;
  width: 400px;
  margin-right: 40px;
  margin-bottom: 20px;
}
.nbsp {
  float: none;
  display: block;
}
.input-box .title {
  display: inline-block;
  margin-right: 7px;
  width: 84px;
  color: #666;
}
.input-box .title i,
.error {
  color: #f56c6c;
}
.input-box .value input {
  height: 34px;
  width: 228px;
  padding: 10px;
}
.error {
  text-align: center;
}
.button-box {
  clear: both;
  border-top: 1px solid #eee;
  padding: 30px;
  margin: 0 auto;
}
.button-box div {
  display: inline-block;
  width: 80px;
  padding: 8px 10px;
  border-radius: 4px;
  text-align: center;
  cursor: pointer;
}
.comfirm {
  color: #fff;
  background-color: #13cb6e;
}
.tip {
  margin-left: 95px;
  color: #f56c6c;
  font-size: 12px;
}
.sm-f {
  padding: 30px;
}
.sm-f > ul {
  display: inline-block;
  margin-right: 15px;
  color: #666;
}
.sm-f .value {
  color: #666;
  font-size: 20px;
  font-weight: bold;
  margin-bottom: 5px;
}
.sm-f .no {
  opacity: 0;
}
.sm-f .button {
  float: right;
  width: 140px;
  text-align: center;
  height: 50px;
  font-size: 20px;
  line-height: 50px;
  color: #fff;
  cursor: pointer;
  border-radius: 4px;
  background-color: #13cb6e;
}
.footer {
  padding: 30px;
  color: #999;
}
.footer span {
  margin-right: 50px;
}
.hint {
  padding: 0 30px 10px;
  color: #999;
}
.el-input,
.el-select {
  width: 228px;
}
.table {
  padding-bottom: 15px;
}
</style>
